﻿using Commons.Responses;
using EntityModels.E02.Identities;
using IRepositories;
using Microsoft.EntityFrameworkCore;
using ORM;

namespace Repositories
{
    public class WeChatRepository : IWeChatRepository
    {
        private readonly PSPDbContext context;

        public WeChatRepository(PSPDbContext context)
        {
            this.context = context;
        }

        public async Task<MessageResponse> CreateWechatUserAsync(WeChatUser weChatUser)
        {
            await context.WeChatUsers.AddAsync(weChatUser);
            try
            {
                await context.SaveChangesAsync();
                return new MessageResponse("创建成功!");
            }
            catch (Exception)
            {

                return new MessageResponse("创建失败!", false);
            }
        }

        public async Task<WeChatUser?> FindByOpenIdAsync(string openId)
        {
            return await context.WeChatUsers.FirstOrDefaultAsync(x => x.OpenId == openId);
        }
    }
}
